from django.db import models

# Create your models here.


class User(models.Model):
    username = models.CharField(max_length=32, verbose_name="用户名")
    password = models.CharField(max_length=32, verbose_name="密码")

    def __str__(self):
        return self.username

class Article(models.Model):

    author = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name="作者")

    title = models.CharField(max_length=32, verbose_name="标题")


    thumbnail = models.ImageField(upload_to="thumbnails",
                                  null=True,
                                  default=None, verbose_name="封面图")

    category = models.ForeignKey("Category",
                                 on_delete=models.CASCADE,
                                 null=True, verbose_name="主要分类")

    subcategory = models.ForeignKey("Subcategory",
                                 on_delete=models.CASCADE,
                                 null=True, verbose_name="次级分类")

    create_time = models.DateTimeField(auto_now_add=True)

    content = models.TextField(verbose_name="文章内容")

    tags = models.ManyToManyField("Tag", verbose_name="标签")

    view_count = models.IntegerField(default=0, verbose_name="阅读量")


    def __str__(self):
        return self.title



class Comment(models.Model):
    article = models.ForeignKey("Article", on_delete=models.CASCADE, verbose_name="被评论文章")

    author = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name="评论者")
    content = models.TextField(verbose_name="评论内容")
    create_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.content[:20]



class Category(models.Model):
    title = models.CharField(max_length=32, verbose_name="主要分类名")

    def __str__(self):
        return self.title



class Subcategory(models.Model):
    title = models.CharField(max_length=32, verbose_name="次级分类名")
    category = models.ForeignKey("Category",
                                 on_delete=models.CASCADE,
                                 verbose_name="所属主要分类")
    def __str__(self):
        return self.title


class Tag(models.Model):
    title = models.CharField(max_length=32, null=True, verbose_name="标签")

    def __str__(self):
        return self.title


class Carousel(models.Model):
    article = models.ForeignKey("Article", on_delete=models.CASCADE, verbose_name="文章")

    image = models.ImageField(upload_to="carousel_images", verbose_name="轮播图")
